package extensions.android;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.modifier.MoveModifier;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;
import org.anddev.andengine.ui.activity.BaseGameActivity;
import org.anddev.andengine.util.modifier.ease.EaseQuintOut;

import extensions.android.andengine.scenes.ServerListScene;

public class RomarioMenu extends BaseGameActivity
{

	private static final int CAMERA_WIDTH = 720;
	private static final int CAMERA_HEIGHT = 480;

	@Override
	public void onLoadComplete()
	{


	}

	@Override
	public Engine onLoadEngine()
	{
		Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		Engine engine = new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera));
		
		return engine;
	}
	
	private TiledTextureRegion joinGameButtonTexture;
	private TextureRegion backButtonTexture;
	private TextureRegion nextButtonTexture;
	private TextureRegion configButtonTexture;
	private TextureRegion playButtonTexture;

	@Override
	public void onLoadResources()
	{
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
		
		BitmapTextureAtlas textureAtlas = new BitmapTextureAtlas(256, 128, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		joinGameButtonTexture = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(textureAtlas, this, "joinGameButton.png", 0, 0, 1, 1);

		BitmapTextureAtlas textureAtlas2 = new BitmapTextureAtlas(128, 128);
		backButtonTexture = BitmapTextureAtlasTextureRegionFactory.createFromAsset(textureAtlas2, this, "backButton.png", 0, 0);
		BitmapTextureAtlas textureAtlas3 = new BitmapTextureAtlas(128, 128);
		nextButtonTexture = BitmapTextureAtlasTextureRegionFactory.createFromAsset(textureAtlas3, this, "nextButton.png", 0, 0);
		BitmapTextureAtlas textureAtlas4 = new BitmapTextureAtlas(128, 128);
		configButtonTexture = BitmapTextureAtlasTextureRegionFactory.createFromAsset(textureAtlas4, this, "configButton.png", 0, 0);
		BitmapTextureAtlas textureAtlas5 = new BitmapTextureAtlas(128, 128);
		playButtonTexture = BitmapTextureAtlasTextureRegionFactory.createFromAsset(textureAtlas5, this, "playButton.png", 0, 0);
		
		getTextureManager().loadTexture(textureAtlas);
		getTextureManager().loadTexture(textureAtlas2);
		getTextureManager().loadTexture(textureAtlas3);
		getTextureManager().loadTexture(textureAtlas4);
		getTextureManager().loadTexture(textureAtlas5);
	}


	@Override
	public Scene onLoadScene()
	{
		Scene scene = new Scene();
		scene.setBackground(new ColorBackground(150/255f, 218/255f, 239/255f));

		final float joinGameSpriteCenterX = CAMERA_WIDTH/2f-joinGameButtonTexture.getWidth()/2;
		final float joinGameSpriteCenterY = CAMERA_HEIGHT/2f-joinGameButtonTexture.getHeight()/2;
		
		//StringInputDialogBuilder inputDialog = new StringInputDialogBuilder(pContext, pTitleResID, pMessageResID, pErrorResID, pIconResID, pSuccessCallback, pOnCancelListener)
		AnimatedSprite joinGameSprite = new AnimatedSprite(-11111, -11111, joinGameButtonTexture)
		{
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY)
			{
				//registerEntityModifier(new MoveModifier(1, -getWidth(), joinGameSpriteCenterX, joinGameSpriteCenterY, joinGameSpriteCenterY, EaseQuintOut.getInstance()));
				getEngine().setScene(new ServerListScene(getEngine()));
				return true;
			}
		};
		
		joinGameSprite.registerEntityModifier(new MoveModifier(1, -joinGameSprite.getWidth(), joinGameSpriteCenterX, joinGameSpriteCenterY, joinGameSpriteCenterY, EaseQuintOut.getInstance()));
		
		//joinGameSprite.animate(100);
		
		//joinGameSprite.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		
		/*joinGameSprite.registerEntityModifier(new SequenceEntityModifier(new RotationModifier(1, 0, 90), new AlphaModifier(2, 1,0), 
				new AlphaModifier(1, 0, 1), new ScaleModifier(2, 1, 0.5f), new DelayModifier(0.5f),
				new ParallelEntityModifier(new ScaleModifier(3, 0.5f, 5), new RotationByModifier(3, 90)),
				new ParallelEntityModifier(new ScaleModifier(3, 5, 1), new RotationModifier(3, 180, 0))));*/
		
		final float backSpriteCenterX = getPosition(0.1f, backButtonTexture, Position.Horizontal, Alignment.Left);
		final float backSpriteCenterY = getPosition(0.9f, backButtonTexture, Position.Vertical, Alignment.Down);
		Sprite backButtonSprite = new Sprite(-1000, backSpriteCenterY, backButtonTexture)
		{
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY)
			{
				
				registerEntityModifier(new MoveModifier(1, -getWidth(), backSpriteCenterX, backSpriteCenterY, backSpriteCenterY, EaseQuintOut.getInstance()));
				return true;
			}
		};
		
		backButtonSprite.registerEntityModifier(new MoveModifier(1, -backButtonTexture.getWidth(), backSpriteCenterX, backSpriteCenterY, backSpriteCenterY, EaseQuintOut.getInstance()));
		
		scene.attachChild(joinGameSprite);
		scene.attachChild(backButtonSprite);
		
		scene.registerTouchArea(joinGameSprite);
		scene.registerTouchArea(backButtonSprite);
		
		return scene;
	}
	
	private float getPosition(float percentage, TextureRegion texture, Position position, Alignment align)
	{		
		float pos; 
		float mod;

		if (position == Position.Horizontal)
		{
			pos = CAMERA_WIDTH*percentage;
			mod = texture.getWidth();
		}else
		{
			pos = CAMERA_HEIGHT*percentage;
			mod = texture.getHeight();			
		}
		
		switch (align)
		{
			case Right:
			case Down:
				pos -= mod;
				break;
			case Center:
				pos -= mod/2f;
				break;
			case Left:
			case Up:
			default:
				break;
		}
		
		return pos;
	}
	
	private static enum Alignment
	{
		Left, Right, Center, Up, Down;
	}
	
	private static enum Position
	{
		Horizontal, Vertical;
	}


}
